package com.linzm.knowledge;

import org.apache.commons.collections.CollectionUtils;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author zimingl
 * @Date 2023/1/14 13:41
 * @Description: TODO
 */
public class CollectionsTest {

    @Test
    public void test1() {
        // 有交集 false
        List<String> list1 = new ArrayList<>();
        list1.add("1");
        List<String> list2 = new ArrayList<>();
        System.out.println(list1.retainAll(list2));
    }

    @Test
    public void test2() {
        List<String> list1 = new ArrayList<>();
        list1.add("1");
        List<String> list2 = new ArrayList<>();
        list2.add(":");
        boolean isSame = list1.contains(list2);
        System.out.println(isSame);
    }

    /**
     * 交集:[11111111111, 22222222222]
     * 去重并集:[55555555555, 11111111111, 22222222222, 66666666666, 33333333333, 44444444444, 77777777777]
     * A系统账号差集:[55555555555, 66666666666]
     * B系统账号差集:[33333333333, 44444444444, 77777777777]
     * 补集：[55555555555, 66666666666, 33333333333, 44444444444, 77777777777]
     */
    @Test
    public void test3() {
        // A系统账号
        List<String> accountList2 = Arrays.asList("11111111111", "22222222222", "33333333333", "44444444444", "77777777777");
        // B系统账号
        List<String> accountList1 = Arrays.asList("55555555555", "11111111111", "22222222222", "66666666666");
        // 账号交集
        List<String> intersectionAccount = accountList1.stream().filter(accountList2::contains).collect(Collectors.toList());
        System.out.println("交集:" + intersectionAccount);
        // 账号去重并集
        List<String> deduplicationUnionAccount = Stream.of(accountList1, accountList2).flatMap(List::stream).distinct().collect(Collectors.toList());
        System.out.println("去重并集:" + deduplicationUnionAccount);
        // A系统账号账号差集(accountList1 - accountList2)
        List<String> subtractionAccount1 = accountList1.stream().filter(account -> !accountList2.contains(account)).collect(Collectors.toList());
        System.out.println("A系统账号差集:" + subtractionAccount1);
        // B系统账号差集(accountList2 - accountList1)
        List<String> subtractionAccount2 = accountList2.stream().filter(account -> !accountList1.contains(account)).collect(Collectors.toList());
        System.out.println("B系统账号差集:" + subtractionAccount2);
        // 账号去重并集
        List<String> deduplicationUnionAccount2= Stream.of(accountList1, accountList2).flatMap(List::stream).distinct().collect(Collectors.toList());
        // 账号交集
        List<String> intersectionAccount2 = accountList1.stream().filter(accountList2::contains).collect(Collectors.toList());
        // 账号补集(并集-交集)
        List<String> complementAccount = deduplicationUnionAccount.stream().filter(account -> !intersectionAccount.contains(account)).collect(Collectors.toList());
        System.out.println("补集：" + complementAccount);
    }

    @Test
    public void test4() {
        ArrayList<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3));
        ArrayList<Integer> list2 = new ArrayList<>( Arrays.asList(3,4,5));
        Collection intersection = CollectionUtils.intersection(list1, list2);
        System.out.println("intersection CollectionUtils 交集结果是： " + intersection);
//        list1.retainAll(list2);
//        System.out.println("intersection retainAll 方法 交集结果是： " + list1);

        Collection union = CollectionUtils.union(list1, list2);
        System.out.println("CollectionUtils 并集结果是： " + union);

        Set<Integer> unionSet = new HashSet<>();
        unionSet.addAll(list1);
        unionSet.addAll(list2);
        System.out.println("set 并集结果是：" + unionSet);

        Collection subtract = CollectionUtils.subtract(list1, list2);
        Collection subtract2 = CollectionUtils.subtract(list2, list1);
        System.out.println("CollectionUtils 差集结果是： " + subtract);
        System.out.println("CollectionUtils 差集结果是： " + subtract2);

        list1.removeAll(list2);
        System.out.println("removeAll 差集结果是： " + list1);
    }

    @Test
    public void test5() {
        new Thread(() -> {
            try {
                TimeUnit.MINUTES.sleep(10);
            } catch (InterruptedException e) {

            }
        }).start();

        ArrayList<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3));
        ArrayList<Integer> list2 = new ArrayList<>( Arrays.asList(3,4,5));
    }

    private String name = "java";
    private String name2 = "java";

    static class Food {}
    static class Fruit extends Food {}

    static class Apple extends Fruit {}

    static class BigApple extends Apple {}
    public static void main(String[] args) throws Exception {
        CollectionsTest collectionsTest = CollectionsTest.class.getConstructor().newInstance();
        Field[] fields = CollectionsTest.class.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.get(collectionsTest));
        }

        //
        List<? super Food> list = new ArrayList<>();
        list.add(new BigApple());
        list.add(new Fruit());
        List<? extends Fruit> list2 = new ArrayList<>();

        String strTxt = "123\\d";
        String s = strTxt.replaceAll("\\d", "456");
        System.out.println(s);
        String s2 = strTxt.replaceAll(Pattern.quote("\\d"), "456");
        System.out.println(s2);
        String s3 = strTxt.replace("\\d", "456");
        System.out.println(s3);

    }

    static class Base {
        public void ming(int x, long y) {

        }

        public void ming(long y, int x) {

        }

        public Base(int num) {
            System.out.println(num);
        }
    }

    static class MyOver extends Base {

        public MyOver(int num) {
            super(num);
        }
    }
}
